
import Comfn from './common'
import config from './config'

export default function (types, url, data, fn, errFn, is_toast, null_toast) {

    let me = this;
    if (!is_toast){ //是否显示加载图
        wx.showToast({
            title: "加载中...",
            icon: "loading",
            duration: 3000
        })
    }else{
      wx.showNavigationBarLoading();
    }

    let access_token = Comfn.getStor('access_token');
    //?access_token=${access_token}
    wx.request({
        url: `${config.defaults_baseURL}${url}`,
        header:{
          'content-type': 'application/x-www-form-urlencoded'
        },
        method: types,
        data: data,
        success: function(res){
            wx.hideToast()
          if (res.data.code == '0') {
                // console.log(res.data.data);
                fn(res.data.data);
			      } else if (res.statusCode == '502' || res.statusCode == '404') {
              wx.showToast({
                title: '服务器错误',
                icon: 'none',
                duration: 2000
              })
            } else if (res.statusCode == '0401' || res.data.resp_code == "0401" ) {
				       if (typeof errFn === 'function') errFn();
                getToken(me);
            } else {
              if (res.data.msg){
                if (typeof errFn === 'function'){
                  errFn(res.data);
                } else {
                  wx.showToast({
                    title: res.data.msg,
                    icon: 'none',
                    duration: 2000
                  })
                  
                }
              } else {
                wx.showToast({
                  title: '未知错误',
                  icon: 'none',
                  duration: 2000
                })
              }
            }
        },
        fail: function(res){
          console.log(res)
            wx.hideToast()
            
          wx.showToast({
            title: '请求发生错误！',
            icon: 'none',
            duration: 2000
          })
        },
        complete:function(){
          wx.hideNavigationBarLoading();
        }
    })
}

let getToken = (value) => {
	let me = value;
    wx.login({
        success: function (res) {
            if (res.code) {
                wx.request({
                    url: `${config.defaults_baseURL}member/token`,
                    method: 'GET',
                    data: { code: res.code },
                    success(res) {
                if(res.data.data.need_bind) {
                  if (me.data.hasOwnProperty('red_packet_id')) {
                    if (me.data.red_packet_id) {
                      // me.receivefirst.showRedBind(); //有红包的情况下显示红包
                      
                    }
                    
                  } else {
                    wx.showModal({
                      title: '提示',
                      content: '检测到您的帐号还未绑定手机号，请绑定手机号！',
                      confirmText: "确定",
                      cancelText: "取消",
                      success: function (res) {
                        if (res.confirm) {
                          //用户
                          if (me.data.hasOwnProperty('scan_shop_id')) {
                            wx.navigateTo({
                              url: '../mobile-verification/mobile-verification?qr_code=1'
                            })
                          } else {
                            wx.navigateTo({
                              url: '../mobile-verification/mobile-verification'
                            })   
                          }
                          
                        } else {
                          wx.switchTab({
                            url: '../index/index'
                          })
                        }
                      }
                    });
                  }
                } else {
                  Comfn.setStor('is_phone', true);
                }
                Comfn.setStor('access_token', res.data.data.access_token || '');
                Comfn.setStor('openId', res.data.data.openid);
                Comfn.setStor('member_id', res.data.data.member_id);
                    }
                })
            } else {
                console.log('wx.login 失败！' + res.errMsg);
            }

        }
    })
}